#include <malloc.h>
#include <core/gpu.h>
#include "logo.h"


static UINT16 RGB24toRGB16(BYTE r, BYTE g, BYTE b)
{
	return (r >> 3) << 11 | (g >> 2) << 5 | b >> 3;
}

static void DrawLogo(UINT16 * const pVideoBuffer, UINT32 uWidth, UINT32 uHeight, EPixFormat ePixFormat)
{
	int i;
	UINT16 *pvBuff;

#if 1
	pvBuff = pVideoBuffer;
	for (i = 0; i < uWidth * (uHeight / 3); i++, pvBuff++)
		*pvBuff = 0x001f;

	for (i = 0; i < uWidth * (uHeight / 3); i++, pvBuff++)
		*pvBuff = 0x07e0;

	for (i = 0; i < uWidth * (uHeight - 2 * (uHeight / 3)); i++, pvBuff++)
		*pvBuff = 0xf800;
#else
	extern const struct Logo gMwImage;

	pvBuff = pVideoBuffer;

	for (i = 0; i < uWidth * uHeight * 3; i += 3)
	{
		*pvBuff = ~ RGB24toRGB16(gMwImage.pixel_data[i],
							gMwImage.pixel_data[i + 1],
							gMwImage.pixel_data[i + 2]
							);
		pvBuff++;
	}
#endif
}

void *VideoMemAlloc(UINT32 uWidth, UINT32 uHeight, EPixFormat ePixFormat, ULONG *pPhyAddr)
{
	void *pBuff;
	UINT32 nSize = uWidth * uHeight;

	switch (ePixFormat)
	{
	case PIX_RGB15:
	case PIX_RGB16:
		nSize *= 2;
		break;

	case PIX_RGB24:
		nSize *= 3;
		break;
	}

	pBuff = DmaMemAlloc(nSize, pPhyAddr);

	// move to the higher level ?:)
	if (pBuff)
	{
		DrawLogo(pBuff, uWidth, uHeight, ePixFormat);
	}

	return pBuff;
}


